home *** CD-ROM | disk | FTP | other *** search
/ NeXTSTEP 3.3 (Developer)…68k, x86, SPARC, PA-RISC] / NeXTSTEP 3.3 Dev Intel.iso / NextDeveloper / Headers / apps / Terminal.h < prev    next >
Text File  |  1995-02-06  |  5KB  |  174 lines

  1. //
  2. // Copyright (C) 1994 by NeXT Computer, Inc.  All rights reserved
  3. // 940413 sam streeper created
  4. //
  5.  
  6. #if 0
  7.  
  8.     This protocol allows applications to request that the Terminal
  9. application perform commands on their behalf.  This is useful if
  10. a program wants to open up a shell window for the user to interact
  11. with, and it may be easier than setting up pipes and doing a 
  12. fork/exec of a program yourself.  The commands may be run in a window,
  13. in which case a window handle is returned to allow a window to be
  14. reused for subsequent commands, or they may be performed in the
  15. background.  Additionally, you can pass in an NXData object whose
  16. data will be fed to the command's stdin, and if you pass in a pointer
  17. to an NXData, Terminal can hand you back the output of the commands
  18. stdout and/or stderr.
  19.  
  20.     Terminal cannot provide services if it is not running.  It may
  21. be necessary to use the workspace request protocol to launch
  22. Terminal if the connection to Terminal fails.
  23.  
  24.     The following protocol is accessed through distributed objects.
  25. For security reasons, the Terminal service is registered with the
  26. bootstrap server rather than the netname server.
  27.  
  28. Terminal's distributed service may be disabled as follows:
  29.     dwrite Terminal DisableDOServices YES
  30.         (or)
  31.     touch /NextApps/Terminal.app/.DisableDOServices
  32.  
  33. You can only get the stdout and stderr of a command if it is run in
  34. the background.  If you pass a pointer to get these back, the data
  35. at the pointer should be nil before invoking the command.  Upon
  36. completion, the pointer will point to an newly created
  37. NXData containing the output.
  38.  
  39. The following example shows how Terminal may be asked to perform a service:
  40.  
  41. //------------------------------
  42. #import <appkit/appkit.h>
  43. #import <mach/mach.h>
  44. #import <mach/mach_error.h>
  45. #import <mach/task_special_ports.h>
  46. #import <servers/bootstrap.h>
  47. #import <machkit/NXPort.h>
  48. #import "/NextDeveloper/Headers/apps/Terminal.h"
  49.  
  50. main()
  51. {
  52.     int i, ret;
  53.     int handle;
  54.     NXData *myOutput=nil, *myEnv=nil;
  55.     id <TSTerminalDOServices> terminal;
  56.     port_t    sport;
  57.     int err = bootstrap_look_up(bootstrap_port, "TerminalDO", &sport);
  58.     NXPort *terminalPort;
  59.     char *cp;
  60.  
  61.     if (err != KERN_SUCCESS) exit(-1);
  62.     terminalPort = [NXPort newFromMachPort:sport];
  63.     terminal = [NXConnection connectToPort:terminalPort];
  64.     if (!terminal) exit(-1);
  65.  
  66.     for (i=0; i<8; i++)
  67.     {
  68.         [terminal runCommand:"echo \"Hello world.\""
  69.             windowType: TSWindowDedicated
  70.             windowHandle: &handle
  71.             shellType:TSShellFastC
  72.             windowTitle:"Echo Test"
  73.             returnCode:&ret];
  74.         sleep(1);
  75.     }
  76.  
  77.     [terminal runCommand:"cd /bin; ls -la"
  78.         windowTitle:"Listing of /bin"];
  79.  
  80.     // the following is bogus; you should compute the size
  81.     // of the environment if you pass it in.  If you pass
  82.     // NULL, however, the env is inherited.
  83.     myEnv = [[NXData alloc] initWithSize:(8192)];
  84.     cp = [myEnv data];
  85.     strcpy(cp,"MESSAGE=Terminal is my friend!");
  86.     while (*cp++);
  87.     strcpy(cp,"PATH=/usr/local/bin:/usr/ucb:/usr/bin:/bin");
  88.     while (*cp++);
  89.     strcpy(cp,"");
  90.  
  91.     myOutput=nil;    // we only want data copied back to us
  92.     [terminal runCommand:"ls -la; echo 'the environment:'; printenv"
  93.         inputData:NULL
  94.         outputData:&myOutput
  95.         errorData:NULL
  96.         waitForReturn:YES            // doesn't matter since there is out data
  97.         windowType: TSWindowNone
  98.         windowHandle: NULL
  99.         exitAction: TSCloseOnExit    // doesn't matter, no window...
  100.         shellType:TSShellBourne
  101.         windowTitle:NULL
  102.         directory:"/etc"
  103.         environment:myEnv
  104.         returnCode:&ret];
  105.     printf("length:%d result is:\n%s\n",[myOutput size],[myOutput data]);
  106. }
  107. //------------------------------
  108.  
  109. #endif
  110.  
  111. #import <objc/Object.h>
  112.  
  113. typedef enum {
  114.     TSWindowNone = 0,
  115.     TSWindowIdle = 2,
  116.     TSWindowNew = 4,
  117.     TSWindowDedicated = 8
  118.     } TSWindowType;
  119.  
  120. typedef enum {
  121.     TSShellNone = 1,
  122.     TSShellBourne = 2,
  123.     TSShellDefault = 4,
  124.     TSShellFastC = 8
  125.     } TSShellType;
  126.  
  127. typedef enum {
  128.     TSCloseOnExit = 0,
  129.     TSCloseUnlessError = 1,
  130.     TSDontCloseOnExit = 2
  131.     } TSExitAction;
  132.  
  133. @protocol TSTerminalDOServices
  134.  
  135. // indicates the version implemented
  136. - (int) protocolVersion;
  137.  
  138. // run in a new window, using fast c shell
  139. - runCommand:(const char *)cmdString
  140.     windowTitle:(const char *)winTitle;
  141.  
  142. // run command in a specified window
  143. - runCommand:(const char *)cmdString
  144.     windowType: (TSWindowType)winType
  145.     windowHandle: (inout int *)myHandle
  146.     shellType:(TSShellType)shellType
  147.     windowTitle:(const char *)winTitle
  148.     returnCode:(out int *)retCode;
  149.  
  150. // run in background using fast c shell, return stdout
  151. - runCommand:(const char *)cmdString
  152.     inputData:inputData                    // an NXData object for stdin
  153.     outputData:(id *)outputData            // a pointer to an NXData object
  154.     waitForReturn:(int)shouldWait        // synchronous even if no out data?
  155.     directory:(const char *)workingDir    // where to cd before execution
  156.     returnCode:(out int *)retCode;
  157.  
  158. // the full form, all others invoke this
  159. - runCommand:(const char *)cmdString
  160.     inputData:(id)inputData
  161.     outputData:(id *)outputData
  162.     errorData:(id *)errorData
  163.     waitForReturn:(int)shouldWait        // synchronous even if no out data?
  164.     windowType: (TSWindowType)winType
  165.     windowHandle: (inout int *)myHandle
  166.     exitAction: (TSExitAction)exitAction
  167.     shellType:(TSShellType)shellType
  168.     windowTitle:(const char *)winTitle
  169.     directory:(const char *)workingDir
  170.     environment:(id)environmentData        // null separated in an NXData
  171.     returnCode:(out int *)retCode;
  172.  
  173. @end
  174.